package main

import (
	"bufio"
	"fmt"
	"log"
	"net"
	"os"
)

func acceptMsg(conn net.Conn) {
	for {
		buf := make([]byte, 4096)
		n, err := conn.Read(buf)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(string(buf[:n]))
	}
}

func main() {
	address, err := net.ResolveTCPAddr("tcp", ":8081")
	if err != nil {
		log.Fatal("listen failed")
	}

	listen, err := net.ListenTCP("tcp", address)
	conn, err := listen.Accept()
	if err != nil {
		log.Fatal("listen failed")
	}

	go acceptMsg(conn)

	input := bufio.NewScanner(os.Stdin)
	for input.Scan() {
		conn.Write([]byte(input.Text()))
	}
}
